<template>
  <div>
    <el-button type="primary" @click="showAddChapterDialog">添加章节</el-button>
    <!-- 章节列表 -->
    <ul class="chapterList">
      <li
        v-for="chapter in chapters"
        :key="chapter.id">
        <p>
          {{ chapter.title }}
          <span class="acts">
            <el-button type="text" @click="handleShowAddVideoDialog(chapter)">添加课时</el-button>
            <el-button type="text" @click="handleShowEditDialog(chapter)">编辑</el-button>
            <el-button type="text" @click="handleDelChapter(chapter)">删除</el-button>
          </span>
        </p>
        <!-- 视频 -->
        <ul class="chapterList videoList">
          <li
            v-for="video in chapter.videos"
            :key="video.id">
            <p>
              {{ video.title }}
              <el-tag v-if="!video.videoSourceId" size="mini" type="danger">
                {{ '尚未上传视频' }}
              </el-tag>
              <span class="acts">
                <el-tag v-if="video.free" size="mini" type="success">{{ '免费观看' }}</el-tag>
                <el-button type="text" @click="handleShowEditVideoDailog(video)">编辑</el-button>
                <el-button type="text" @click="handleDelVideo(video)">删除</el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>

    <div style="text-align:center">
      <el-button :disabled="flag" type="primary" plain @click="prev">上一步</el-button>
      <el-button :disabled="flag" type="primary" plain @click="next">下一步</el-button>
    </div>
    <chapter-form ref="chapterDialog"/>
    <video-form ref="videoDialog"/>
  </div>
</template>

<script>
import ChapterForm from '@/views/course/components/chapter/ChapterForm'
import VideoForm from '@/views/course/components/chapter/VideoForm'
import chapterApi from '@/api/chapter'
import videoApi from '@/api/video'
export default {
  components: {
    ChapterForm, VideoForm
  },
  data() {
    return {
      flag: false,
      chapters: []
    }
  },
  created() {
    this.getChapters()
  },
  methods: {
    // 编辑课时回显单击事件
    handleShowEditVideoDailog(video) {
      this.$refs.videoDialog.title = '更新课时'
      this.$refs.videoDialog.open(video.id)
    },
    // 删除课时单击事件
    handleDelVideo(video) {
      this.$confirm(`此操作将永久删除【${video.title}】课时, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 用户点击确认按钮的回调
        videoApi.delete(video.id).then(r => {
          this.$message.success('删除课时成功')
          this.getChapters()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加课时对话框
    handleShowAddVideoDialog(chapter) {
      // console.log(this.chapter)
      this.$refs.videoDialog.title = '新增课时'
      this.$refs.videoDialog.video = {
        sort: 0,
        free: 0,
        // 保存课时 需要给它绑定它的 课程和章的id
        courseId: this.$parent.courseId,
        chapterId: chapter.id
      }
      this.$refs.videoDialog.dialogFormVisible = true
    },
    // 显示编辑章的对话框回显章数据的单击事件
    handleShowEditDialog(chapter) {
      // 清空表单的缓存
      this.$refs.chapterDialog.chapter = {
        sort: 0
      }
      this.$refs.chapterDialog.title = '更新章节'
      // 在chapterFrom中提供一个方法，接受章的id，然后查询章的数据回显到对话框中然后显示
      this.$refs.chapterDialog.open(chapter.id)
    },
    // 删除章
    handleDelChapter(chapter) {
      this.$confirm(`此操作将永久删除【${chapter.title}】章节, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => { // 用户点击确认按钮的回调
        chapterApi.delete(chapter.id).then(r => {
          this.$message.success('删除章节成功')
          this.getChapters()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getChapters() {
      chapterApi.getChapters(this.$parent.courseId).then(r => {
        this.chapters = r.data.items
      })
    },
    showAddChapterDialog() {
      // 清空表单的缓存
      this.$refs.chapterDialog.chapter = {
        sort: 0
      }
      // 弹出一个收集章的对话框表单
      this.$refs.chapterDialog.title = '新增章节'
      this.$refs.chapterDialog.dialogFormVisible = true
    },
    prev() {
      if (this.flag) {
        return
      }
      this.flag = true
      // 跳转到下一个页面：只需要修改父vue页面中的form.vue中的active的值
      /**
      * vue父子组件通信：
      *     1、父组件获取子组件；调用方法
      *         this.$refs.ref值.属性/方法()
      *     2、子组件获取父组件
      *         this.$parent
      */
      this.$parent.active = 0
    },
    next() {
      if (this.flag) {
        return
      }
      this.flag = true
      // 跳转到下一个页面：只需要修改父vue页面中的form.vue中的active的值
      /**
      * vue父子组件通信：
      *     1、父组件获取子组件；调用方法
      *         this.$refs.ref值.属性/方法()
      *     2、子组件获取父组件
      *         this.$parent
      */
      this.$parent.active = 2
    }
  }
}
</script>

<style>
.chapterList{
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
}
.chapterList li{
  position: relative;
}
.chapterList p{
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}
.chapterList .acts {
    float: right;
    font-size: 14px;
}

.videoList{
  padding-left: 50px;
}
.videoList p{
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dashed #DDD;
}
</style>

